package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class RabbitConfig {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @PostConstruct
    public void initConfirmCallback(){
        System.out.println("开始设置ConfirmCallback");
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
//            CorrelationData correlationData, 消息的uuid
//            boolean b, 是否已送达至borker
//            String s 错误消息是
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("correlationData==>"+correlationData+"ask==>"+b+"错误消息为："+s);
            }
        });
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
//            Message message, 失败的消息内容
//            int i,  失败码
//            String s, 失败信息
//            String s1,交换机名称
//            String s2 路由键
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("message=>"+message+"code=>"+i+"text=>"+s+"exchange=>"+s1+"routeKey=>"+s2);
            }
        });
    }
}
